// 2025/10/04
// Author: Forever
#include <bits/stdc++.h>
using namespace std;
#define int long long

vector<pair<int, int>> edges[100001];
int n, m, s, dist[100001];
bool inq[100001];

void spfa(int s)
{
    memset(dist, 0x3f, sizeof dist);
    dist[s] = 0, inq[s] = true;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        inq[u] = false;
        for (auto ed : edges[u]) {
            int v = ed.first, w = ed.second;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                if (!inq[v]) {
                    q.push(v);
                    inq[v] = true;
                }
            }
        }
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        edges[u].push_back({ v, w });
    }
    spfa(s);
    for (int i = 1; i <= n; i++)
        cout << (dist[i] == 0x3f3f3f3f3f3f3f3f ? INT_MAX : dist[i]) << " ";
    return 0;
}